package com.sgb.jicai.view.activity;

import android.content.Context;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import com.sgb.jicai.R;
import com.sgb.jicai.databinding.ActivitySimpleListBinding;
import com.sgb.jicai.entity.JcChanceEntity;
import com.sgb.jicai.view.adapter.JcChanceAdapter;
import com.sgb.jicai.viewmodel.JcChanceListModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;

/**
 * type:1 报价机遇、2 加盟机遇列表
 */
public class JcChanceListActivity extends AppActivity {

    private ActivitySimpleListBinding binding;
    JcChanceListModel viewModel;
    private JcChanceAdapter adapter;
    private int type;
    private boolean isNexts;

    public static void start(Context context, int type) {
        Intent intent = new Intent(context, JcChanceListActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }
    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_simple_list);
        viewModel = new ViewModelProvider(this).get(JcChanceListModel.class);
        viewModel.setActivity(JcChanceListActivity.this);
        binding.include.ivBack.setOnClickListener(this);
        type = getIntent().getIntExtra("type", 1);
        binding.include.titleView.setText(type == 1 ? "报价机遇" : "加盟机遇");
        if (adapter == null) {
            adapter = new JcChanceAdapter(this, type, new ArrayList<>());
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.list.setLayoutManager(layoutManager);
        binding.list.setAdapter(adapter);
        binding.list.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                init();
            }

            @Override
            public void onLoadMore() {
                viewModel.getChance(type, true);
            }
        });
        adapter.setItemClickCallBack(new JcChanceAdapter.ItemClickCallBack() {
            @Override
            public void onItemClick(JcChanceEntity entity) {
                viewModel.getH5Url(entity.getId(), entity.getGroupBuyMode());
            }
        });
        binding.imgBackTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.list.scrollToPosition(0);
            }
        });

        binding.list.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {//SCROLL_STATE_IDLE:停止滚动
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (layoutManager.findFirstVisibleItemPosition() > 1) {
                    binding.imgBackTop.setVisibility(View.VISIBLE);
                } else {
                    binding.imgBackTop.setVisibility(View.GONE);
                }
            }
        });
    }

    public void getH5URL(JcChanceEntity entity) {
        viewModel.getH5Url(entity.getId(), entity.getGroupBuyMode());
    }

    @Override
    protected void init() {
        viewModel.getChance(type, false);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_back) {
            finish();
        }
    }

    public void getListComplete() {
        binding.list.refreshComplete();
        binding.list.loadMoreComplete();
    }


    //返回数据信息
    public void setList(List<JcChanceEntity> list, boolean isLoadMore) {
        if (isLoadMore) {
            binding.list.loadMoreComplete();
            if (list.size() >0) {
                adapter.addDatas(list);
            }else {
                binding.list.setNoMore(true);
            }
        } else {
            if (list.size()> 0) {
                binding.emptyView.setVisibility(View.GONE);
                binding.list.setVisibility(View.VISIBLE);
                adapter.setDatas(list);
                if (list.size()<10&&list.size()>=3){
                    binding.list.setNoMore(true);
                }else {
                    binding.list.setNoMore(false);
                }
            } else {
                adapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.imgBackTop.setVisibility(View.GONE);
                binding.list.setVisibility(View.GONE);
            }
        }
    }
}
